Javascript: Pause while a condition is true